/**
 * @ClassName FileController.java
 * @Desc ToDo
 * @Author TimDong
 * @CreateTime: 2015年5月4日 下午7:07:13
 */
package com.omni.web.controllers;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
/**
 * @ClassName FileController.java
 * @Desc ToDo
 * @Author TimDong
 * @CreateTime: 2015年5月4日 下午7:07:13
 */
@Controller
@RequestMapping("/file")
public class FileController extends BaseController {
	
	    @RequestMapping(value="/singleUpload")
	    public ModelAndView singleUpload(){
	    	return new ModelAndView("file/singleUpload");
	    }
	    
	    @RequestMapping(value="/singleSave", method=RequestMethod.POST )
	    public @ResponseBody String singleSave(@RequestParam("file") MultipartFile file, @RequestParam("desc") String desc ){
	    	System.out.println("File Description:"+desc);
	    	String fileName = null;
	    	if (!file.isEmpty()) {
	            try {
	                fileName = file.getOriginalFilename();
	                byte[] bytes = file.getBytes();
	                BufferedOutputStream buffStream = 
	                        new BufferedOutputStream(new FileOutputStream(new File("F:/cp/" + fileName)));
	                buffStream.write(bytes);
	                buffStream.close();
	                return "You have successfully uploaded " + fileName;
	            } catch (Exception e) {
	                return "You failed to upload " + fileName + ": " + e.getMessage();
	            }
	        } else {
	            return "Unable to upload. File is empty.";
	        }
	    }
	    @RequestMapping(value="/multipleUpload")
	    public ModelAndView multiUpload(){
	    	return new ModelAndView("file/multipleUpload");
	    }
	    @RequestMapping(value="/multipleSave", method=RequestMethod.POST )
	    public @ResponseBody String multipleSave(@RequestParam("file") MultipartFile[] files){
	    	String fileName = null;
	    	String msg = "";
	    	if (files != null && files.length >0) {
	    		for(int i =0 ;i< files.length; i++){
		            try {
		                fileName = files[i].getOriginalFilename();
		                byte[] bytes = files[i].getBytes();
		                BufferedOutputStream buffStream = 
		                        new BufferedOutputStream(new FileOutputStream(new File("F:/cp/" + fileName)));
		                buffStream.write(bytes);
		                buffStream.close();
		                msg += "You have successfully uploaded " + fileName +"<br/>";
		            } catch (Exception e) {
		                return "You failed to upload " + fileName + ": " + e.getMessage() +"<br/>";
		            }
	    		}
	    		return msg;
	        } else {
	            return "Unable to upload. File is empty.";
	        }
	    }
	} 	
		
		
	/*
	//判断表单是否是Multipart类型的。这里可以直接对request进行判断 
	if (ServletFileUpload.isMultipartContent(request)) {
	    // 创建文件处理工厂，它用于生成 FileItem 对象。 
	    DiskFileItemFactory factory = new DiskFileItemFactory(); 
	     //设置文件的缓存路径
	       String tempdir =this.getServletContext().getRealPath("/upload/temp");    
	     java.io.File d = new java.io.File(savedir);
	        if(!d.exists()){
	         d.mkdirs();
	        }
	      
	    factory.setSizeThreshold(1024*1024); // 设置最多只允许在内存中存储的数据,单位:字节
	    factory.setRepository(d); // 设置一旦文件大小超过getSizeThreshold()的值时数据存放在硬盘的目录(默认可以不用设置)
	 
	    // Create a new file upload handler
	    ServletFileUpload upload = new ServletFileUpload(factory);
	    // 设置允许用户上传文件大小,单位:字节
	    upload.setSizeMax(yourMaxRequestSize);
	    //上传文件,并解析出所有的表单字段，包括普通字段和文件字段
	    List  items = upload.parseRequest(request); 
	         //下面对每个字段进行处理，分普通字段和文件字段
	   Iterator it = items.iterator();
	   while(it.hasNext()){
	    FileItem fileItem = (FileItem) it.next();
	    //如果是普通字段
	    if(fileItem.isFormField()){  //是普通的字段
	     System.out.println(fileItem.getFieldName() + "   " + fileItem.getName() + "   " + new String(fileItem.getString().getBytes("iso8859-1"), "gbk"));
	    fileItem.getFieldName();//得到字段name属性的值
	    fileItem.getName();//得到file字段的文件名全路径名，如果不是file字段，为null
	    fileItem.getString();//得到该字段的值,默认的编码格式
	    fileItem.getString("UTF-8");//指定编码格式
	    }else{//文件字段
	         System.out.println(fileItem.getFieldName() + "   " +
	        fileItem.getName() + "   " +//得到file字段的文件名全路径名
	        fileItem.isInMemory() + "    " +//用来判断FileItem类对象封装的主体内容是存储在内存中，还是存储在临时文件中，如果存储在内存中则返回true，否则返回false
	        fileItem.getContentType() + "   " +//文件类型
	        fileItem.getSize());          //文件大小
	 
	       //什么东西都有了 ，想怎么处理都可以了
	     //保存文件，其实就是把缓存里的数据写到目标路径下
	     if(fileItem.getName()!=null && fileItem.getSize()!=0){
	      File fullFile = new File(fileItem.getName());
	      File newFile = new File("c:/temp/" + fullFile.getName());
	      try {
	       fileItem.write(newFile);
	      } catch (Exception e) {
	       e.printStackTrace();
	      }
	     }else{
	      System.out.println("文件没有选择 或 文件内容为空");
	     }
	    }
	*/
	

